package com.sinosoft.perfect.system._roleSystem.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sinosoft.perfect.system._.po.TRoleSystem;
import com.sinosoft.perfect.system._.po.TUserRolesysRelation;
import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._roleSystem.dao.IRoleSystemDAO;
import com.sinosoft.perfect.system._roleSystem.vo.AllUsersInOneRoleSystemVO;
import com.sinosoft.perfect.system._roleSystem.vo.RoleSystemVO;

public class RoleSystemServiceImpl implements IRoleSystemService {

	private IRoleSystemDAO roleSystemDAO;

	public void setRoleSystemDAO(IRoleSystemDAO roleSystemDAO) {
		this.roleSystemDAO = roleSystemDAO;
	}

	public void deleteTRoleSystemByPrimaryKey(TRoleSystem troleSystem) throws SQLException {
		roleSystemDAO.deleteTRoleSystemByPrimaryKey(troleSystem);
		roleSystemDAO.deleteTRoleSystemUserByRoleSystemPrimaryKey(troleSystem);
		roleSystemDAO.deleteTRoleSystemUnitByRoleSystemPrimaryKey(troleSystem);
	}

	public String insertByTRoleSystem(TRoleSystem troleSystem) throws SQLException {
		String id = Producer.generateIdOfTen();
		troleSystem.setRoleSystemId(id);
		troleSystem.setStatus("1");
		roleSystemDAO.insertByTRoleSystem(troleSystem);
		return id;
	}

	public List<TRoleSystem> selectByTRoleSystem(TRoleSystem troleSystem) throws SQLException {
		return roleSystemDAO.selectByTRoleSystem(troleSystem);
	}

	public List<String> selectTRoleSystemFromUnit(String unitId) throws SQLException {
		List<RoleSystemVO> roleSystemVOs = roleSystemDAO.selectTRoleSystemFromUnit(unitId);
		List<String> roleSystemIds = null;
		if (roleSystemVOs != null && roleSystemVOs.size() != 0) {
			roleSystemIds = new ArrayList<String>();
			for (int i = 0; i < roleSystemVOs.size(); i++) {
				roleSystemIds.add(roleSystemVOs.get(i).getRoleSysId());
			}
		}
		return roleSystemIds;
	}

	public List<String> selectTRoleSystemFromUser(String userId) throws SQLException {
		List<RoleSystemVO> roleSystemVOs = roleSystemDAO.selectTRoleSystemFromUser(userId);
		List<String> roleSystemIds = null;
		if (roleSystemVOs != null && roleSystemVOs.size() != 0) {
			roleSystemIds = new ArrayList<String>();
			for (int i = 0; i < roleSystemVOs.size(); i++) {
				roleSystemIds.add(roleSystemVOs.get(i).getRoleSysId());
			}
		}
		return roleSystemIds;
	}

	public void updateTRoleSystemByPrimaryKey(TRoleSystem troleSystem) throws SQLException {
		roleSystemDAO.updateTRoleSystemNotNullByPrimaryKey(troleSystem);
	}

	@Override
	public String selectExitsUsers(TRoleSystem troleSystem) throws SQLException {
		List<AllUsersInOneRoleSystemVO> allUsersInOneRoleSystemVOs =  roleSystemDAO.selectExitsUsers(troleSystem);
		StringBuffer userIds = new StringBuffer("");
		if (allUsersInOneRoleSystemVOs != null && allUsersInOneRoleSystemVOs.size() > 0 ) {
			for (int i = 0; i < allUsersInOneRoleSystemVOs.size(); i++) {
				AllUsersInOneRoleSystemVO allUsersInOneRoleSystemVO = allUsersInOneRoleSystemVOs.get(i);
				if(allUsersInOneRoleSystemVO != null && null != allUsersInOneRoleSystemVO.getUserMainId() && !"".equals(allUsersInOneRoleSystemVO.getUserMainId())){
					userIds.append(allUsersInOneRoleSystemVOs.get(i).getUserMainId());
					if(i < allUsersInOneRoleSystemVOs.size()-1){
						userIds.append(",");
					}
				}
			}
		}
		return userIds.toString();
	}

	@Override
	public void deleteTUserRoleSystem(TUserRolesysRelation tUserRolesysRelation) throws Exception {
		roleSystemDAO.deleteTUserRoleSystem(tUserRolesysRelation);
	}

}
